
package customers;

import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import model.DBManager;
import model.Vehicle;

/**
 *
 * @author Team-2
 */
public class VehicleDialog extends javax.swing.JFrame {

     private final EntityManager em;


    /**
     * Creates new form VehicleDialog
     */
    public VehicleDialog( Vehicle v, boolean readOnly) {

        em = DBManager.em;
        vehicle=v;
        if ( !em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        model.Vehicle vehicleModel = vehicle;
        categoryBoxRenderer = new avenue.CategoryBoxRenderer();
        vehicleCategoryQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT v FROM VehicleCategory v");
        vehicleCategoryList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(vehicleCategoryQuery.getResultList());
        programBoxRender = new avenue.ProgramBoxRender();
        programQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Program p");
        programList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(programQuery.getResultList());
        jTextField3 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        vehicleTab = new javax.swing.JPanel();
        modelText = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        vehicleCategoryCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        branchCombo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        colourCombo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        regNumberText = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        cardTab = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        barcodeText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        programCombo = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        cardIdText = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        balanceText = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

        categoryBoxRenderer.setText("categoryBoxRenderer1");

        programBoxRender.setText("programBoxRender1");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${factoryName}"), jTextField3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Διαχείριση Οχήματος");
        setAlwaysOnTop(true);
        setResizable(false);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Εφαρμογή");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Ακυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${modelType}"), modelText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        modelText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modelTextActionPerformed(evt);
            }
        });

        jLabel7.setText("Μοντέλο: ");

        vehicleCategoryCombo.setRenderer(categoryBoxRenderer);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleCategoryList, vehicleCategoryCombo);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${categoryId}"), vehicleCategoryCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        vehicleCategoryCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vehicleCategoryComboActionPerformed(evt);
            }
        });

        jLabel3.setText("Κατηγορία :  ");

        branchCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALFA ROMEO", "AUDI", "BMW", "BOVA", "CITROEN", "FIAT", "FORD", "ISUZU", "LADA", "MAN", "MERCEDES", "NISSAN", "OPEL", "PIAGGIO", "RENAULT", "SCANIA", "SUZUKI", "VOLVO", "VOLKSWAGEN", "YAMAHA", "" }));
        branchCombo.setSelectedIndex(-1);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, branchCombo, org.jdesktop.beansbinding.ObjectProperty.create(), branchCombo, org.jdesktop.beansbinding.BeanProperty.create("elements"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${factoryName}"), branchCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        branchCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                branchComboActionPerformed(evt);
            }
        });

        jLabel4.setText("Κατασκευαστής: ");

        colourCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ΑΣΗΜΙ", "ΑΣΠΡΟ", "ΓΑΛΑΖΙΟ", "ΓΚΡΙ", "ΚΑΦΕ", "ΚΙΤΡΙΝΟ", "ΚΟΚΚΙΝΟ", "ΜΑΥΡΟ", "ΜΠΛΕ", "ΠΡΑΣΙΝΟ", "ΠΟΡΤΟΚΑΛΙ" }));
        colourCombo.setSelectedIndex(-1);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, colourCombo, org.jdesktop.beansbinding.ObjectProperty.create(), colourCombo, org.jdesktop.beansbinding.BeanProperty.create("elements"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${colour}"), colourCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        colourCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colourComboActionPerformed(evt);
            }
        });

        jLabel5.setText("Χρώμα: ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${regNumber}"), regNumberText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel1.setText("Αρ. Πινακίδας:  ");

        jLabel11.setText("*");

        jLabel12.setText("*");

        javax.swing.GroupLayout vehicleTabLayout = new javax.swing.GroupLayout(vehicleTab);
        vehicleTab.setLayout(vehicleTabLayout);
        vehicleTabLayout.setHorizontalGroup(
            vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vehicleTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(vehicleTabLayout.createSequentialGroup()
                        .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(vehicleTabLayout.createSequentialGroup()
                                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(branchCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, 145, Short.MAX_VALUE)
                                    .addComponent(colourCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(modelText, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vehicleCategoryCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(vehicleTabLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(regNumberText)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(23, 23, 23))
        );
        vehicleTabLayout.setVerticalGroup(
            vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vehicleTabLayout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(regNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(vehicleCategoryCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(branchCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(modelText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vehicleTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(colourCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jTabbedPane2.addTab("Όχημα", vehicleTab);

        jLabel2.setText("Barcode :  ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${cardId.barcode}"), barcodeText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        barcodeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                barcodeTextActionPerformed(evt);
            }
        });

        jLabel6.setText("Πρόγραμμα :  ");

        programCombo.setRenderer(programBoxRender);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, programList, programCombo);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${cardId.programId}"), programCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        programCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                programComboActionPerformed(evt);
            }
        });

        jLabel8.setText("Aρ. Κάρτας :");

        cardIdText.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${cardId.id}"), cardIdText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel9.setText("Υπόλοιπο :");

        balanceText.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleModel, org.jdesktop.beansbinding.ELProperty.create("${cardId.balance}"), balanceText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel10.setText("*");

        jLabel13.setText("*");

        javax.swing.GroupLayout cardTabLayout = new javax.swing.GroupLayout(cardTab);
        cardTab.setLayout(cardTabLayout);
        cardTabLayout.setHorizontalGroup(
            cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cardTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2)
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(barcodeText)
                    .addComponent(programCombo, 0, 130, Short.MAX_VALUE)
                    .addComponent(cardIdText)
                    .addComponent(balanceText))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        cardTabLayout.setVerticalGroup(
            cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cardTabLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cardIdText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(barcodeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(balanceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(cardTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(programCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Κάρτα", cardTab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if ( validateFields() ) {
            MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,true);
               for (WindowListener l : this.getWindowListeners()) {
                   l.windowClosed(we);
              }
              this.setVisible(false);      
        }      
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
   
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,false);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
       }
       this.setVisible(false); 
    }//GEN-LAST:event_cancelButtonActionPerformed

/**
 * Validate some required fields
 * @return 
 */    
  private boolean validateFields() {
        
       String  message;
       boolean res = true;
       String regNumber = regNumberText.getText();
       String barcode = barcodeText.getText();
       
        //required reg Number
        if (regNumber.isEmpty()) {
             message = "Δεν συμπληρώσατε αριθμό πινακίδας, παρακαλώ ξαναπροσπαθήστε";
             JOptionPane.showMessageDialog(this, message, "Προσοχή", 0); 
             return false;
        }    

       //not selected vehicle category
       if (vehicleCategoryCombo.getSelectedItem()==null) {
           message = "Δεν επιλέξατε κατηγορία οχήματος, παρακαλώ ξαναπροσπαθήστε";  
           JOptionPane.showMessageDialog(this, message, "Προσοχή", 0); 
           return false;
       }
       
        //required card barcode
        if (barcode.isEmpty()) {
             message = "Δεν συμπληρώσατε barcode κάρτας, παρακαλώ ξαναπροσπαθήστε";
             JOptionPane.showMessageDialog(this, message, "Προσοχή", 0); 
             return false;
        }         
       
       //not selected program
       if (programCombo.getSelectedItem()==null) {
           message = "Δεν επιλέξατε πρόγραμμα, παρακαλώ επιλέξτε";  
           JOptionPane.showMessageDialog(this, message, "Προσοχή", 0); 
           return false;
       }   
       
      return res;
    }    
    
    
    private void modelTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modelTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_modelTextActionPerformed

    private void barcodeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_barcodeTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_barcodeTextActionPerformed

    private void vehicleCategoryComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vehicleCategoryComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vehicleCategoryComboActionPerformed

    private void programComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_programComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_programComboActionPerformed

    private void branchComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_branchComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_branchComboActionPerformed

    private void colourComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colourComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_colourComboActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VehicleDialog dialog = new VehicleDialog( new Vehicle(), false);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField balanceText;
    private javax.swing.JTextField barcodeText;
    public static javax.swing.JComboBox branchCombo;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField cardIdText;
    private javax.swing.JPanel cardTab;
    private avenue.CategoryBoxRenderer categoryBoxRenderer;
    private javax.swing.JComboBox colourCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField modelText;
    private avenue.ProgramBoxRender programBoxRender;
    private javax.swing.JComboBox programCombo;
    private java.util.List<model.Program> programList;
    private javax.persistence.Query programQuery;
    private javax.swing.JTextField regNumberText;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox vehicleCategoryCombo;
    private java.util.List<model.VehicleCategory> vehicleCategoryList;
    private javax.persistence.Query vehicleCategoryQuery;
    private javax.swing.JPanel vehicleTab;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    Vehicle vehicle;
}
